//
//  LYEmoticonPackage.swift
//  EmoticonKeyboard
//
//  Created by liuerying on 16/5/31.
//  Copyright © 2016年 liuerying. All rights reserved.
// 表情的分组模型

import UIKit

class LYEmoticonPackage: NSObject {
    
    var title :String?
    
    var sectionEmoticon:[[LYEmoticon]] = [[LYEmoticon]]()
    
    
    init(id:String,title:String,array : [[String: String]]) {
        self.title = title
        super.init()
        
        var tempArray = [LYEmoticon]()
        var index = 0

        for item in array{
        
            let e = LYEmoticon(id: id, dict: item)
//            print(e.chs)
            
            tempArray.append(e)
            
            index += 1
            
            if index == 20{
            
                let delete = LYEmoticon(isDelete: true)
                tempArray.append(delete)
                
                index = 0
            }
        }
        
        let delta = tempArray.count % 21
        
        if delta != 0 {
        
            for _ in delta..<20{
            
                let space = LYEmoticon(isEmpty: true)
                tempArray.append(space)
            }
            
            let delete = LYEmoticon(isDelete: true)
            
            tempArray.append(delete)
        }
        //转为二维数组
        sectionEmoticon = sectionEmoticons(tempArray)
    }
    
    //需要讲分组的表情的数据的数组处理成二维数组
    private func sectionEmoticons(array:[LYEmoticon]) ->[[LYEmoticon]]{
    
        let pageCount = (array.count - 1) / EmotionPageCount + 1
        
        var sectionArr = [[LYEmoticon]]()
        
        for i in 0..<pageCount{
        
            let loc = i * EmotionPageCount
            var length = EmotionPageCount
            
            if loc + length > array.count {
            
                length = array.count - loc
            }
            let rang  = NSMakeRange(loc, length)
            let subArray  = (array as NSArray).subarrayWithRange(rang)
            
            sectionArr.append(subArray as! [LYEmoticon])
        }
        
        return sectionArr
        
    }

}
